<template>
  <div class="music-ranking">
    <!-- 标签页导航 -->
    <div class="ranking-tabs">
      <button 
        class="tab-button" 
        :class="{ active: activeTab === 'songs' }"
        @click="activeTab = 'songs'"
      >
        热门曲目
      </button>
      <button 
        class="tab-button" 
        :class="{ active: activeTab === 'singers' }"
        @click="activeTab = 'singers'"
      >
        热门歌手
      </button>
    </div>

    <!-- 热门曲目列表 -->
    <div v-if="activeTab === 'songs'" class="ranking-content">
      <h3 class="ranking-title">🔥 热门歌曲排行榜</h3>
      <div class="ranking-list">
        <div 
          v-for="song in topSongs" 
          :key="song.id"
          class="ranking-item song-item"
          @click="playRankingSong(song)"
        >
          <!-- 排名数字 -->
          <div class="rank-number" :class="{ top: song.rank <= 3 }">
            {{ song.rank }}
          </div>
          
          <!-- 封面 -->
          <div class="song-cover">
            <img :src="song.cover" :alt="song.title" class="cover-image">
            <div class="play-overlay">
              <button class="btn-play-small" @click.stop="playRankingSong(song)">▶️</button>
            </div>
          </div>
          
          <!-- 歌曲信息 -->
          <div class="song-detail">
            <div class="song-title-wrapper">
              <h4 class="song-title">{{ song.title }}</h4>
              <button class="btn-play" @click.stop="playRankingSong(song)">▶️</button>
            </div>
            <p class="song-artist">{{ song.artist }}</p>
            <p class="play-count">{{ formatPlayCount(song.playCount) }}</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 热门歌手列表 -->
    <div v-else class="ranking-content">
      <h3 class="ranking-title">🎤 热门歌手排行榜</h3>
      <div class="ranking-list singers-list">
        <div 
          v-for="singer in topSingers" 
          :key="singer.id"
          class="ranking-item singer-item"
        >
          <!-- 排名数字 -->
          <div class="rank-number" :class="{ top: singer.rank <= 3 }">
            {{ singer.rank }}
          </div>
          
          <!-- 歌手头像 -->
          <div class="singer-avatar">
            <img :src="singer.avatar" :alt="singer.name" class="avatar-image">
          </div>
          
          <!-- 歌手信息 -->
          <div class="singer-detail">
            <h4 class="singer-name">{{ singer.name }}</h4>
            <p class="singer-stats">
              <span class="song-count">{{ singer.songCount }} 首歌曲</span>
              <span class="fans-count">{{ formatFanCount(singer.fansCount) }} 粉丝</span>
            </p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useMusicStore } from '../stores/music.js'

const store = useMusicStore()

// 标签页状态
const activeTab = ref('songs')

// 从store获取排行榜数据
const topSongs = computed(() => store.topSongs)
const topSingers = computed(() => store.topSingers)

// 播放排行榜歌曲
const playRankingSong = (song) => {
  // 检查歌曲是否已在播放列表中
  const existingSongIndex = store.playlist.findIndex(item => item.id === song.id)
  
  if (existingSongIndex >= 0) {
    // 如果已存在，则直接播放
    store.currentIndex = existingSongIndex
    store.isPlaying = true
  } else {
    // 如果不存在，则添加到播放列表并播放
    store.addSong(song)
    store.currentIndex = store.playlist.length - 1
    store.isPlaying = true
  }
  
  // 通知播放器组件更新
  window.dispatchEvent(new Event('songChanged'))
}

// 格式化播放次数
const formatPlayCount = (count) => {
  if (count >= 100000000) {
    return (count / 100000000).toFixed(1) + '亿'
  } else if (count >= 10000) {
    return (count / 10000).toFixed(1) + '万'
  }
  return count.toString()
}

// 格式化粉丝数量
const formatFanCount = (count) => {
  if (count >= 100000000) {
    return (count / 100000000).toFixed(1) + '亿'
  } else if (count >= 10000) {
    return (count / 10000).toFixed(1) + '万'
  }
  return count.toString()
}
</script>

<style scoped>
.music-ranking {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 30px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  max-width: 1200px;
  margin: 0 auto;
}

/* 标签页样式 */
.ranking-tabs {
  display: flex;
  gap: 20px;
  margin-bottom: 30px;
  border-bottom: 2px solid #f0f0f0;
}

.tab-button {
  padding: 12px 30px;
  background: none;
  border: none;
  font-size: 18px;
  font-weight: 500;
  color: #666;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  border-radius: 8px 8px 0 0;
}

.tab-button:hover {
  color: #e53e3e;
  background: rgba(229, 62, 62, 0.05);
}

.tab-button.active {
  color: #e53e3e;
  font-weight: 600;
}

.tab-button.active::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  right: 0;
  height: 2px;
  background: #e53e3e;
}

/* 标题样式 */
.ranking-title {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 25px;
  color: #333;
  text-align: center;
}

/* 排行榜列表样式 */
.ranking-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
  max-height: 600px;
  overflow-y: auto;
}

.ranking-item {
  display: flex;
  align-items: center;
  padding: 15px;
  background: white;
  border-radius: 12px;
  transition: all 0.3s ease;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.ranking-item:hover {
  transform: translateX(5px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}

/* 排名数字样式 */
.rank-number {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: 700;
  color: #999;
  margin-right: 20px;
  border-radius: 50%;
  background: #f5f5f5;
}

.rank-number.top {
  background: linear-gradient(135deg, #e53e3e 0%, #c53030 100%);
  color: white;
}

/* 歌曲项样式 */
.song-item {
  gap: 20px;
}

.song-cover {
  width: 80px;
  height: 80px;
  position: relative;
  border-radius: 8px;
  overflow: hidden;
  flex-shrink: 0;
}

.cover-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.song-cover:hover .cover-image {
  transform: scale(1.05);
}

.play-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.song-cover:hover .play-overlay {
  opacity: 1;
}

.btn-play-small {
  background: white;
  border: none;
  border-radius: 50%;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 14px;
}

.btn-play-small:hover {
  transform: scale(1.1);
  background: #e53e3e;
  color: white;
}

.song-detail {
  flex: 1;
  min-width: 0;
}

.song-title-wrapper {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 8px;
}

.song-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.btn-play {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #999;
  transition: color 0.3s ease;
  padding: 0;
}

.btn-play:hover {
  color: #e53e3e;
}

.ranking-title {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 25px;
  text-align: center;
  background: linear-gradient(135deg, #e53e3e 0%, #c53030 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.song-artist {
  font-size: 14px;
  color: #666;
  margin: 0 0 5px 0;
}

.play-count {
  font-size: 12px;
  color: #999;
  margin: 0;
}

/* 歌手项样式 */
.singers-list {
  max-height: 500px;
}

.singer-item {
  gap: 20px;
}

.singer-avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  overflow: hidden;
  flex-shrink: 0;
  border: 3px solid #f0f0f0;
}

.avatar-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.singer-item:hover .avatar-image {
  transform: scale(1.05);
}

.singer-detail {
  flex: 1;
}

.singer-name {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0 0 10px 0;
}

.singer-stats {
  display: flex;
  gap: 20px;
  font-size: 14px;
  color: #666;
  margin: 0;
}

.song-count, .fans-count {
  display: flex;
  align-items: center;
  gap: 5px;
}

/* 滚动条样式 */
.ranking-list::-webkit-scrollbar {
  width: 8px;
}

.ranking-list::-webkit-scrollbar-track {
  background: #fff0f0;
  border-radius: 4px;
}

.ranking-list::-webkit-scrollbar-thumb {
  background: #e53e3e;
  border-radius: 4px;
}

.ranking-list::-webkit-scrollbar-thumb:hover {
  background: #c53030;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .music-ranking {
    padding: 20px;
  }
  
  .ranking-tabs {
    gap: 10px;
  }
  
  .tab-button {
    padding: 10px 20px;
    font-size: 16px;
  }
  
  .ranking-title {
    font-size: 20px;
    margin-bottom: 20px;
  }
  
  .ranking-item {
    padding: 12px;
    gap: 15px;
  }
  
  .rank-number {
    width: 32px;
    height: 32px;
    font-size: 16px;
    margin-right: 15px;
  }
  
  .song-cover, .singer-avatar {
    width: 60px;
    height: 60px;
  }
  
  .song-title, .singer-name {
    font-size: 16px;
  }
  
  .singer-stats {
    flex-direction: column;
    gap: 5px;
  }
}
</style>